/**********************************
Salario no census 2010 
* daily wages in 2010= 252  & 252/(435.36301/934.34198)=540.82266 (in 2022)
* hourly wages in 2010=53 & 53 /(435.36301/934.34198)=113.74445 (in 2022)
*/
*******************************
* Mechanical Costs 
* estimation  sample 
use  "${Gpath}\data\\estimation_sample.dta", clear 
keep if main_sample==1 
keep sp_cnes date_adm
bysort sp_cnes date_adm: keep if _n==1 

tempfile A 
save `A'.dta, replace 

* nonintensity-based policy 
use  "${Gpath}\data\\rellocated.dta", clear 
merge m:1 date_adm sp_cnes using `A'.dta, nogen keep(3) 
keep cns date_adm
bysort cns date_adm: keep if _n==1 

gen dow=dow(date_adm )
gen Dsalary=(dow==0 | dow==6)*(540.82266)
replace Dsalary=(540.82266) if (month(date_adm)==1 & day(date_adm)==1) ///
| (month(date_adm)==4 & day(date_adm)==21)  ///
| (month(date_adm)==5 & day(date_adm)==1)  ///
| (month(date_adm)==9 & day(date_adm)==7 ) ///
| (month(date_adm)==10 & day(date_adm)==12 ) ///
| (month(date_adm)==11 & day(date_adm)==2 ) ///
| (month(date_adm)==12 & day(date_adm)==25) 

sum Dsalary
global Mecani_costs=r(sum)

*************************************************************************************************
* Mortality and cost reductions 
*************************************************************************************************
use  "${Gpath}\data\\estimation_sample.dta", clear 
keep if main_sample==1 

reghdfe  val_tot  share_dcbo_* $XVar0 $Xvar0_phy overlap , ///
a(cpfxcnesxyear hospitalxdow hospitalxmonth)  
global betaVAL=_b[overlap]

reghdfe  m_30d  share_dcbo_* $XVar0 $Xvar0_phy overlap , ///
a(cpfxcnesxyear hospitalxdow hospitalxmonth)  
global betaM=_b[overlap]

gen cns_operator=procedUnic
merge m:1 sp_cnes date_adm cns_operator using "${Gpath}\data\\\overlap_available_operator_hours_long.dta", nogen keep(1 3)
merge m:1 sp_cnes date_adm  using "${Gpath}\data\\predicted_covariates.dta", nogen keep(1 3)
merge m:1 sp_cnes date_adm cns_operator using "${Gpath}\data\\overlap_available_operator_rellocating.dta", nogen keep(1 3)

egen idxcnes=group(cns_operator sp_cnes)

local a=_N 
gen ys1=${betaVAL}*(predicted_roverlap -predicted_hwoverlap)*`a'/10

gen yc1=${betaM}*(predicted_roverlap -predicted_hwoverlap)*`a'

sum yc1 
global lives_saved=abs(r(mean))

sum ys1
global externality=abs(r(mean))

global VSL=${lives_saved}*80837.25*13 

global MPVF =${VSL}/(${Mecani_costs}-${externality})

mat B=(${lives_saved}, ${VSL}, ${externality} , ${Mecani_costs}, ${MPVF})

clear 
set obs 1 
gen lives_saved=round(${lives_saved})
gen VSL="${VSL}"
gen externality="${externality}" 
gen mechanical_cost="${Mecani_costs}"
gen MPVF="${MPVF}"

outsheet   using "${Gpath}\\table\\counterfactual.csv",   replace delimiter(";")